home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / libguile / throw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-04  |  5.8 KB  |  259 lines

  1. /*    Copyright (C) 1995 Free Software Foundation, Inc.
  2.  * 
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2, or (at your option)
  6.  * any later version.
  7.  * 
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this software; see the file COPYING.  If not, write to
  15.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  *
  17.  * As a special exception, the Free Software Foundation gives permission
  18.  * for additional uses of the text contained in its release of GUILE.
  19.  *
  20.  * The exception is that, if you link the GUILE library with other files
  21.  * to produce an executable, this does not by itself cause the
  22.  * resulting executable to be covered by the GNU General Public License.
  23.  * Your use of that executable is in no way restricted on account of
  24.  * linking the GUILE library code into it.
  25.  *
  26.  * This exception does not however invalidate any other reasons why
  27.  * the executable file might be covered by the GNU General Public License.
  28.  *
  29.  * This exception applies only to the code released by the
  30.  * Free Software Foundation under the name GUILE.  If you copy
  31.  * code from other Free Software Foundation releases into a copy of
  32.  * GUILE, as the General Public License permits, the exception does
  33.  * not apply to the code that you add in this way.  To avoid misleading
  34.  * anyone as to the status of such modified files, you must delete
  35.  * this exception notice from them.
  36.  *
  37.  * If you write modifications of your own for GUILE, it is your choice
  38.  * whether to permit this exception to apply to your modifications.
  39.  * If you do not wish that, delete this exception notice.  
  40.  */
  41.  
  42.  
  43. #include <stdio.h>
  44. #include "_scm.h"
  45.  
  46.  
  47.  
  48.  
  49. /* {Catch and Throw} 
  50.  */
  51. static int tc16_jmpbuffer;
  52.  
  53. #define JMPBUFP(O) (TYP16(O) == tc16_jmpbuffer)
  54. #define JBACTIVE(O) (CAR (O) & (1L << 16L))
  55. #define ACTIVATEJB(O)  (CAR (O) |= (1L << 16L))
  56. #define DEACTIVATEJB(O)  (CAR (O) &= ~(1L << 16L))
  57. #define JBJMPBUF(O) ((jmp_buf*)CDR (O) )
  58.  
  59.  
  60. #ifdef __STDC__
  61. static int
  62. printjb (SCM exp, SCM port, int writing)
  63. #else
  64. static int
  65. printjb (exp, port, writing)
  66.      SCM exp;
  67.      SCM port;
  68.      int writing;
  69. #endif
  70. {
  71.   scm_puts ("#<jmpbuffer ", port);
  72.   scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
  73.   scm_intprint(CDR(exp), 16, port);
  74.   scm_putc ('>', port);
  75.   return 1 ;
  76. }
  77.  
  78. static scm_smobfuns jbsmob = {scm_mark0, scm_free0, printjb, 0};
  79.  
  80. #ifdef __STDC__
  81. static SCM
  82. make_jmpbuf (void)
  83. #else
  84. static SCM
  85. make_jmpbuf ()
  86. #endif
  87. {
  88.   SCM answer;
  89.   NEWCELL (answer);
  90.   DEFER_INTS;
  91.   {
  92.     CAR(answer) = tc16_jmpbuffer;
  93.     JBJMPBUF(answer) = (jmp_buf *)0;
  94.     DEACTIVATEJB(answer);
  95.   }
  96.   ALLOW_INTS;
  97.   return answer;
  98. }
  99.  
  100.  
  101.  
  102. PROC (s_catch, "catch", 3, 0, 0, scm_catch);
  103. #ifdef __STDC__
  104. SCM
  105. scm_catch (SCM tag, SCM thunk, SCM handler)
  106. #else
  107. SCM
  108. scm_catch (tag, thunk, handler)
  109.      SCM tag;
  110.      SCM thunk;
  111.      SCM handler;
  112. #endif
  113. {
  114.   jmp_buf buf;
  115.   SCM jmpbuf;
  116.   SCM answer;
  117.  
  118.   ASSERT ((tag == BOOL_F) || (NIMP(tag) && SYMBOLP(tag)) || (tag == BOOL_T),
  119.       tag, ARG1, s_catch);
  120.   jmpbuf = make_jmpbuf ();
  121.   answer = EOL;
  122.   dynwinds = scm_acons (tag, jmpbuf, dynwinds);
  123.   JBJMPBUF(jmpbuf) = &buf;
  124.   if (setjmp (buf))
  125.     {
  126.       SCM throw_args;
  127.       DEFER_INTS;
  128.       DEACTIVATEJB (jmpbuf);
  129.       dynwinds = CDR (dynwinds);
  130.       ALLOW_INTS;
  131.       throw_args = scm_throwval;
  132.       scm_throwval = EOL;
  133.       answer = scm_apply (handler, scm_cons (tag, throw_args), EOL);
  134.     }
  135.   else
  136.     {
  137.       ACTIVATEJB (jmpbuf);
  138.       answer = scm_apply (thunk,
  139.               ((tag == BOOL_F) ? scm_cons (jmpbuf, EOL) : EOL),
  140.               EOL);
  141.       DEFER_INTS;
  142.       DEACTIVATEJB (jmpbuf);
  143.       dynwinds = CDR (dynwinds);
  144.       ALLOW_INTS;
  145.     }
  146.   return answer;
  147. }
  148.  
  149.  
  150. static char s_throw[];
  151. SCM scm_bad_throw_vcell;
  152. #ifdef __STDC__
  153. SCM
  154. _scm_throw (SCM key, SCM args, int noreturn)
  155. #else
  156. SCM
  157. _scm_throw (key, args, noreturn)
  158.      SCM key;
  159.      SCM args;
  160.      int noreturn;
  161. #endif
  162. {
  163.   SCM jmpbuf;
  164.   if (NIMP (key) && JMPBUFP (key))
  165.     {
  166.       jmpbuf = key;
  167.       if (noreturn)
  168.     {
  169.       ASSERT (JBACTIVE (jmpbuf), jmpbuf,
  170.           "throw to dynamicly inactive catch",
  171.           s_throw);
  172.     }
  173.       else if (!JBACTIVE (jmpbuf))
  174.     return UNSPECIFIED;
  175.     }
  176.   else
  177.     {
  178.       SCM dynpair;
  179.       if (noreturn)
  180.     {
  181.       ASSERT (NIMP (key) && SYMBOLP (key), key, ARG1, s_throw);
  182.     }
  183.       else if (!(NIMP (key) && SYMBOLP (key)))
  184.     return UNSPECIFIED;
  185.  
  186.       dynpair = scm_assoc (key, dynwinds);
  187.  
  188.       if (dynpair == BOOL_F)
  189.     dynpair = scm_assoc (BOOL_T, dynwinds);
  190.  
  191.       if ((dynpair == BOOL_F)
  192.       && (BOOL_T == scm_procedurep (CDR (scm_bad_throw_vcell))))
  193.     {
  194.       SCM answer;
  195.       answer = scm_apply (CDR (scm_bad_throw_vcell), scm_cons (key, args), EOL);
  196.     }
  197.       
  198.       if (noreturn)
  199.     {
  200.       ASSERT (dynpair != BOOL_F,
  201.           scm_cons (key, args),
  202.           "missing CATCH", s_throw);
  203.     }
  204.       else if (dynpair == BOOL_F)
  205.     return UNSPECIFIED;
  206.  
  207.       jmpbuf = CDR (dynpair);
  208.     }
  209.   scm_throwval = args;
  210.   longjmp (*JBJMPBUF (jmpbuf), 1);
  211. }
  212.  
  213.  
  214.  
  215. PROC (s_throw, "throw", 1, 0, 1, scm_throw_exception);
  216. #ifdef __STDC__
  217. SCM
  218. scm_throw_exception (SCM key, SCM args)
  219. #else
  220. SCM
  221. scm_throw_exception (key, args)
  222.      SCM key;
  223.      SCM args;
  224. #endif
  225. {
  226.   _scm_throw (key, args, 1);
  227.   return BOOL_F;  /* never really returns */
  228. }
  229.  
  230.  
  231.  
  232.  
  233. PROC (s_dynamic_root, "dynamic-root", 0, 0, 0, scm_dynamic_root);
  234. #ifdef __STDC__
  235. SCM
  236. scm_dynamic_root (void)
  237. #else
  238. SCM
  239. scm_dynamic_root ()
  240. #endif
  241. {
  242.   return scm_ulong2num (SEQ (rootcont));
  243. }
  244.  
  245.  
  246.  
  247. #ifdef __STDC__
  248. void
  249. scm_init_throw (void)
  250. #else
  251. void
  252. scm_init_throw ()
  253. #endif
  254. {
  255.   tc16_jmpbuffer = scm_newsmob (&jbsmob);
  256. #include "throw.x"
  257. }
  258.  
  259.